package com.slfx.easy.poi.demo;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.slfx.easy.poi.entity.Member;
import com.slfx.easy.poi.entity.MemberVerifyMode;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

/**
 * <p>简单模板导入</p> <br>
 * 文档：http://doc.wupaas.com/docs/easypoi/easypoi-1c0u6ksp2r091 <br>
 * gitee:  https://gitee.com/lemur/easypoi
 *
 * @author slfx
 */
public class ExcelImportSimpleMember {

    @Test
    public void importMember() {
        Date start = new Date();
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        try {
            // MultipartFile
            File file = new File("D://home/easypoi//exportMember.xlsx");
            List<Member> result = ExcelImportUtil.importExcel(file, Member.class, params);
            System.out.println("耗时：" + (new Date().getTime() - start.getTime()));
            for (int i = 0; i < result.size(); i++) {
                System.out.println(result.get(i).toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void importMemberWithVerify() {
        Date start = new Date();
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        params.setNeedVerify(true);
        try {
            // MultipartFile
            File file = new File("D://home/easypoi//exportMember.xlsx");
            ExcelImportResult<MemberVerifyMode> result = ExcelImportUtil.importExcelMore(file, MemberVerifyMode.class, params);
            System.out.println("是否存在错误：" + result.isVerifyFail());
            FileOutputStream fos = new FileOutputStream("D://home/easypoi//importMemberVerifyTest.xlsx");
            result.getFailWorkbook().write(fos);
            fos.close();

            List<MemberVerifyMode> memberList = result.getList();
            System.out.println("耗时：" + (new Date().getTime() - start.getTime()));
            for (int i = 0; i < memberList.size(); i++) {
                System.out.println(memberList.get(i).toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void importBigDataMember() {
        Date start = new Date();
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        params.setConcurrentTask(true);
        try {
            // MultipartFile
            File file = new File("D://home/easypoi//exportMemberBigData.xlsx");
            List<Member> result = ExcelImportUtil.importExcel(file, Member.class, params);

            System.out.println(result.size());
            System.out.println("耗时：" + (new Date().getTime() - start.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
